package com.jic.market.mapper.coupon;

import com.jic.market.entity.coupon.CouponConfig;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 优惠券配置表
 *
 * @author : seven
 * @version : v1.0.0
 * @email : 774329481@qq.com
 * @since : 2020-03-15 08:18:04
 */
@Repository
public interface CouponConfigMapper {

    long insert(CouponConfig couponConfig);

    Long delete(String promotionId);

    long update(CouponConfig couponConfig);

    CouponConfig queryByPrimaryKey(Long id);

    List<CouponConfig> query(CouponConfig couponConfig);

    List<CouponConfig> queryPage(@Param("startIndex") int startIndex, @Param("limit") int limit, @Param("couponConfig") CouponConfig couponConfig);

    long queryPageCount(CouponConfig couponConfig);

    @Select("select * from market_coupon_config where promotion_id=#{promotionId} and delete_flag='0'")
    CouponConfig queryByPromotionId(@Param("promotionId")String promotionId);

    @Update("update market_coupon_config set trigger_num=#{triggerNum} where promotion_id=#{promotionId} and delete_flag='0'")
    Long updateTriggerNumByPromotionId(@Param("promotionId")String promotionId,
                                       @Param("triggerNum")String triggerNum);

    @Update("update market_coupon_config set grant_num=grant_num+#{grantNum} where promotion_id=#{promotionId} and delete_flag='0' and status='5'")
    Long updateGrantNum(@Param("promotionId")String promotionId,
                        @Param("grantNum")Integer grantNum);

    @Select("select a.* from market_coupon_config as a left join market_coupon_detail as b on(a.promotion_id=b.promotion_id) where b.coupon_code=#{couponCode} and a.delete_flag='0'")
    CouponConfig queryByCouponConfigByCode(@Param("couponCode")String couponCode);
}